package com.ruifu.act.config;

import com.google.common.collect.Lists;
import com.ruifu.act.listener.MultipleUserTaskParseHandler;
import com.ruifu.act.listener.ProcessEventListener;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.parse.BpmnParseHandler;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@Slf4j
public class GlobalActivityListenerConfig implements ProcessEngineConfigurationConfigurer {

    @Autowired(required = false)
    ProcessEventListener processEventListener;

    @Autowired(required = false)
    MultipleUserTaskParseHandler multipleUserTaskParseHandler;

    @Override
    public void configure(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
        List<ActivitiEventListener> eventListeners = springProcessEngineConfiguration.getEventListeners();
        if (null == eventListeners) {
            eventListeners = Lists.newArrayList();
            springProcessEngineConfiguration.setEventListeners(eventListeners);
        }
        eventListeners.add(processEventListener);

        log.info("Activiti-全局监听器已经被注入配置");

        ///
        List<BpmnParseHandler> handlers = springProcessEngineConfiguration.getPostBpmnParseHandlers();
        if (null == handlers) {
            handlers = Lists.newArrayList();
            springProcessEngineConfiguration.setPostBpmnParseHandlers(handlers);
        }
        handlers.add(multipleUserTaskParseHandler);

        log.info("Activiti-全局用户分配监听器已经被注入配置");


    }
}
